package com.zaizai.zaizai_good.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zaizai.zaizai_good.domain.dto.UserDto;
import com.zaizai.zaizai_good.domain.pojo.User;
import com.zaizai.zaizai_good.service.UserService;
import com.zaizai.zaizai_good.mapper.UserMapper;
import com.zaizai.zaizai_good.utils.Md5Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 86178
 * @description 针对表【user(人员表)】的数据库操作Service实现
 * @createDate 2024-10-03 10:00:08
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Integer register(UserDto userDto) {
        //先检验账号是否重复
        User u = userMapper.findUserByUsername(userDto.getUsername());
        //账号不重复
        if (u == null) {
            //判断密码是否一致
            if (userDto.getPassword().equals(userDto.getRePassword())) {
                //密码一致
                User user = new User();
                BeanUtils.copyProperties(userDto, user);
                //密码加密
                user.setPassword(Md5Util.getMD5String(userDto.getPassword()));
                userMapper.insertUser(user);
                return 1;
            }
            return -1; //密码不一致
        }
        return -2; //账号重复
    }

    @Override
    public User login(UserDto userDto) {
        //查询账号
        User user = userMapper.findUserByUsername(userDto.getUsername());
        //判断账号是否存在
        if (user != null) {
            //账号存在，判断密码是否一致
            if (Md5Util.checkPassword(userDto.getPassword(), user.getPassword())) {
                //密码一致，登录成功
                return user;
            }
        }
        return null;
    }

    /**
     * 查询人员
     *
     * @param id 人员主键
     * @return 人员
     */
    @Override
    public User selectUserById(Long id) {
        return userMapper.selectUserById(id);
    }

    /**
     * 查询人员列表
     *
     * @param user 人员
     * @return 人员
     */
    @Override
    public List<User> selectUserList(User user) {
        return userMapper.selectUserList(user);
    }

    /**
     * 新增人员
     *
     * @param user 人员
     * @return 结果
     */
    @Override
    public int insertUser(User user) {
        user.setPassword(Md5Util.getMD5String("123456"));
        return userMapper.insertUser(user);
    }

    /**
     * 修改人员
     *
     * @param user 人员
     * @return 结果
     */
    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    /**
     * 批量删除人员
     *
     * @param ids 需要删除的人员主键
     * @return 结果
     */
    @Override
    public int deleteUserByIds(Long[] ids) {
        return userMapper.deleteUserByIds(ids);
    }

    /**
     * 删除人员信息
     *
     * @param id 人员主键
     * @return 结果
     */
    @Override
    public int deleteUserById(Long id) {
        return userMapper.deleteUserById(id);
    }
}




